package com.example.mam.shiro;

import com.example.mam.entity.Manager;
import com.example.mam.mapper.ManagerMapper;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

import javax.annotation.Resource;


/**
 * @author muncie [muncie.hoo@qq.com]
 */
public class ShiroRealm extends AuthorizingRealm {
    @Resource
    private ManagerMapper managerMapper;


    @Override
    public Class getAuthenticationTokenClass() {
        return super.getAuthenticationTokenClass();
    }

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return new SimpleAuthorizationInfo();
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken)
            throws AuthenticationException {
        String principal = (String) authenticationToken.getPrincipal();
        Manager manager = managerMapper.selectManagerByEmail(principal);
        if (manager == null) {
            return null;
        }
        return new SimpleAuthenticationInfo(manager.getId(), manager.getPassword(), getName());
    }
}
